package Lib3d.Physics;


import Lib3d.Vector3d;
import Lib3d.PresetModel3d.Sphere;
import Lib3d.SceneGraph.Impl.ObjectNodeImpl;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

/**
 * An implementation of sphere collision. This class is not used by the current
 * version.
 * 
 * @author Wassim Chegham
 * @version 0.1
 * @category Lib3d.Physics
 * 
 */
public class SphereCollision extends ObjectNodeImpl {

    private final float d = 0;

    private final Vector3d s1center, s2center;

    private final float s1radius, s2radius;

    public SphereCollision(final Sphere s1, final Sphere s2) {
        this.s1radius = s1.getRadius();
        this.s2radius = s2.getRadius();
        this.s1center = s1.getSphereCenter();
        this.s2center = s2.getSphereCenter();
    }

    @Override
    public void draw(final GLAutoDrawable gLDrawable) {
        super.draw(gLDrawable);
        final GL gl = gLDrawable.getGL();
        gl.glPushMatrix();
        if (this.d <= this.s2radius + this.s1radius) {
            gl.glColor3f(1, 0, 0);
        }
        else {
            gl.glColor3f(0, 0, 1);
        }
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex3f(this.s1center.get(0), this.s1center.get(1), this.s1center.get(2));
        gl.glEnd();
        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glColor3f(0, 1, 0);
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex3f(this.s1center.get(0), this.s1center.get(1), this.s1center.get(2));
        gl.glEnd();
        gl.glPopMatrix();

    }

    public float testCollision() {
        final float s1x = this.s1center.get(0);
        final float s1y = this.s1center.get(1);
        final float s1z = this.s1center.get(2);
        final float s2x = this.s1center.get(0);
        final float s2y = this.s1center.get(1);
        final float s2z = this.s1center.get(2);
        return (float) Math.sqrt(((s1x - s2x) * (s1x - s2x))
                + ((s1y - s2y) * (s1y - s2y)) + ((s1z - s2z) * (s1z - s2z)));
    }
}
